package com.iseastar.guojiang.newcabinet.adapter;


import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.iseastar.guojiang.model.NewParcelBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;

/**
 * 包裹列表照片图片适配器
 * Created by lidandan on 2017/12/22.
 */

public class NoScrollGridViewAdapter extends BaseAdapterExt<String> {

    private Handler handler;
    private int parentPosition;
    private NewParcelBean parcelBean;

    public NoScrollGridViewAdapter(ArrayList<String> items, Activity context, Handler handler, int parentPosition, NewParcelBean parcelBean) {
        super(items, context);
        this.handler = handler;
        this.parentPosition = parentPosition;
        this.parcelBean = parcelBean;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null || convertView.getTag() == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.no_scroll_grid_view_item, parent, false);
            viewHolder.closeIv = (ImageView) convertView.findViewById(R.id.close_iv);
            viewHolder.bagIv = (ImageView) convertView.findViewById(R.id.bag_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //删除拍照后的图片
        viewHolder.closeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                parcelBean.setDeleteParcelImage(getItem(position));
                setItemOnClick(0, parentPosition, position, parcelBean);
            }
        });
        if (!StringUtils.isEmpty(getItem(position))) {
            GlideUtil.loadImage(context, getItem(position), 0, viewHolder.bagIv);
            //点击查看包裹图片
            viewHolder.bagIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (parcelBean != null) {
                        setItemOnClick(5, parentPosition, position, parcelBean);
                    }
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        ImageView closeIv;//关闭
        ImageView bagIv;//包裹图片
    }

    /**
     * @param what
     * @param parentPosition
     * @param parcelBean
     */
    private void setItemOnClick(int what, int parentPosition, int imagePosition, NewParcelBean parcelBean) {
        Message message = Message.obtain();
        message.what = what;
        message.arg1 = parentPosition;
        message.arg2 = imagePosition;
        message.obj = parcelBean;
        handler.sendMessage(message);
    }
}
